package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrStorehouseDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.LmbAllotApplyDAO;
import com.trkj.dao.jpa.LmbAllotApplyDetaliedDAO;
import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.pojos.LmbAllotApply;
import com.trkj.pojos.LmbAllotApplyDetalied;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.vo.LmbDbVo;
import com.trkj.vo.LmbhcdbVo;

@Service
@Transactional
public class LmbHcdbService {
	@Autowired
	private LmbConsumptiveStockDAO consumptiveStockDAO;
	@Autowired
	private LmbAllotApplyDAO applyDAO;
	@Autowired
	private LmbAllotApplyDetaliedDAO detaliedDAO;
	@Autowired
	private LmbOddNumbersService oddNumbersService;
	@Autowired
	private IYzrTbstaffDAO tbstaffDAO;
	@Autowired
	private IYzrStorehouseDAO storehouseDAO;
	
	public void doHcdb(LmbhcdbVo hcdbVo){
		//保存调拨的基本信息
		// 获取日期
		String date = oddNumbersService.rq();
		//拼接
		String name = "HD-" + date;					
		String n = applyDAO.findMaxID(name);	
		System.out.println("从数据库中读取的最大采购计划编号："+n);
		String sid =oddNumbersService.getId(name, n);
		LmbAllotApply apply =  hcdbVo.getAllotApply();
		apply.setAllotId(sid);
		applyDAO.save(apply);
		//保存调拨详细
		List<LmbAllotApplyDetalied> list = hcdbVo.getApplyDet();
		System.out.println("list的长度："+list.size());
		if(list != null){	
			for(LmbAllotApplyDetalied detail:list){
				//拼接
				String name1 = "HDX-" + date;	
				String n1 = detaliedDAO.findMaxID(name1);	
				String sid1 =oddNumbersService.getId(name1, n1);
				detail.setAAllotId(sid);
				detail.setADetaliedId(sid1);
				System.out.println(detail.getACid());
				System.out.println(detail.getABatchNum());
				detaliedDAO.save(detail);
				//修改库存信息
				//找出调拨仓
				String dbckId = apply.getAllotStorehouse();
				//找出调拨到的仓库
				String dbzckId = apply.getAllotToStorehouse();
				//根据调拨仓Id,耗材编号和批号找到相对应的库存
				LmbConsumptiveStock stock = consumptiveStockDAO.findByConStock(dbckId, detail.getACid(), detail.getABatchNum());
				//获取的库存数量
				BigDecimal num = stock.getCstocNum();
				//获取的diaobo数量
				BigDecimal num1 = detail.getANum();
				//调拨之后的库存数量
				BigDecimal cknum = num.subtract(num1);
				stock.setCstocNum(cknum);
				//根据调拨至仓Id,耗材编号和批号找到相对应的库存
				LmbConsumptiveStock stock1 = consumptiveStockDAO.findByConStock(dbzckId, detail.getACid(), detail.getABatchNum());
				if(stock1!=null){
					//获取的库存数量
					BigDecimal dnum = stock1.getCstocNum();
					//调拨之后的库存数量
					BigDecimal dcknum = dnum.add(num1);
					stock1.setCstocNum(dcknum);
					consumptiveStockDAO.save(stock1);
				}else{
					String name2 = "HK-" + date;
					String n2 = consumptiveStockDAO.findMaxID(name2);
					String sid2 = oddNumbersService.getId(name2, n2);
					LmbConsumptiveStock cs = new LmbConsumptiveStock();
					cs.setCstockId(sid2);
					cs.setCstocBatchNum(stock.getCstocBatchNum());
					cs.setCstocCid(stock.getCstocCid());
					cs.setCstocCname(stock.getCstocCname());
					cs.setCstockSupplier(stock.getCstockSupplier());
					cs.setCstockValidTill(stock.getCstockValidTill());
					cs.setCstockProductDate(stock.getCstockProductDate());
					cs.setCstocNum(detail.getANum());
					cs.setCstocNorms(stock.getCstocNorms());
					cs.setCstocWarehouseId(apply.getAllotToStorehouse());
					consumptiveStockDAO.save(cs);
				}				
			}					
		}					
	}
	
	
	public LmbDbVo dbPoToVo(LmbAllotApply apply){
		LmbDbVo dbVo = new LmbDbVo();
		dbVo.setAllotId(apply.getAllotId());
		dbVo.setAllotApplyTime(apply.getAllotApplyTime());
		dbVo.setAllotCgy(apply.getAllotCgy());//tbstaffDAO
		Tbstaff staff = tbstaffDAO.findOne(apply.getAllotCgy().longValue());
		dbVo.setCgName(staff.getStaffName());
		dbVo.setAllotStorehouse(apply.getAllotStorehouse());
		YzrStorehouse storehouse = storehouseDAO.findOne(apply.getAllotStorehouse());
		dbVo.setDbcName(storehouse.getStoreName());
		dbVo.setAllotToStorehouse(apply.getAllotToStorehouse());
		YzrStorehouse tostorehouse = storehouseDAO.findOne(apply.getAllotToStorehouse());
		dbVo.setDbtoName(tostorehouse.getStoreName());
		return dbVo;		
	}
	
	public List<LmbDbVo> dbPoListToVoList(List<LmbAllotApply> list){
		if(list == null) return null;
		List<LmbDbVo> listVo = new ArrayList<>();
		for(LmbAllotApply apply: list){
			LmbDbVo dbVo = dbPoToVo(apply);
			listVo.add(dbVo);
		}
		return listVo;
	}
	
	public Map<String,Object> findDbByPage(int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbAllotApply> list = applyDAO.findAll(pr);
		long total = applyDAO.countfindAll();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", dbPoListToVoList(list));
		return map;				
	}
	
	public List<LmbDbVo> findAllDb(){
		List<LmbAllotApply> list = applyDAO.findAllApply();
		return dbPoListToVoList(list);		
	}
	
	public List<LmbAllotApplyDetalied> findXqBydbId(String id){
		return detaliedDAO.findDetaliedByApplyId(id);		
	}
	
	public LmbDbVo findOneDb(String id){
		LmbAllotApply list = applyDAO.findOne(id);
		return  dbPoToVo(list);
	}
}
